/**
 * \file led.c
 * \brief Driver des LEDs et du BarLED.
 * \author Pierrick.C
 * \version 0.9
 * \date 22 février 2011
 *
 * Fichier d'implémentation des APIs de la LED status et du BarLED.
 *
 */
 
 #include <msp430x14x.h>
#include <io.h>
#include <signal.h>
#include <iomacros.h>


#include "led.h"
//#include "../utils/utils.h"


#define			D_ON				P6OUT |= BIT0     	//P6.0 // D:data du multiplexer
#define			D_OFF				P6OUT &= ~BIT0     	//P6.0


#define			SEL_ON				P6OUT |= BIT7     	//P6.7 // LE: selection de mode:-adressable latch
#define			SEL_OFF				P6OUT &= ~BIT7     	//P6.7							-memory

#define			S0_ON				P6OUT |= BIT1     	//P6.1  // A0: adresse du multiplexer
#define			S0_OFF				P6OUT &= ~BIT1     	//P6.1
#define			S1_ON				P6OUT |= BIT2     	//P6.2	// A1
#define			S1_OFF				P6OUT &= ~BIT2     	//P6.2
#define			S2_ON				P6OUT |= BIT3     	//P6.3	// A2
#define			S2_OFF				P6OUT &= ~BIT3     	//P6.3

#define         STATUS_LED_ON      P2OUT &= ~BIT1    //Allume la LED STATUS
#define         STATUS_LED_OFF     P2OUT |= BIT1     //Eteint la LED STATUS

void initLedBar(void)
{
	P2DIR = ~BIT0;                                // enable only P2.0 as output
}


void allumerLedBar(int num)
{
		
	
	if ((num>8) || (num<0))
	SEL_ON;	
	else
	{
		
		switch (num)
		{
			case (1): 
			{	S0_OFF;		//line selection 1/8 (msb) S2 S1 S0 (lsb)
				S1_OFF;
				S2_OFF;
				break;
			}
			case (2): 
			{	S0_ON;		//line selection 1/8 (msb) S2 S1 S0 (lsb)
				S1_OFF;
				S2_OFF;
				break;
			}
			case (3): 
			{	S0_OFF;		//line selection 1/8 (msb) S2 S1 S0 (lsb)
				S1_ON;
				S2_OFF;
				break;
			}
			case (4): 
			{	S0_ON;		//line selection 1/8 (msb) S2 S1 S0 (lsb)
				S1_ON;
				S2_OFF;
				break;
			}
			case (5): 
			{	S0_OFF;		//line selection 1/8 (msb) S2 S1 S0 (lsb)
				S1_OFF;
				S2_ON;
				break;
			}
			case (6): 
			{	S0_ON;		//line selection 1/8 (msb) S2 S1 S0 (lsb)
				S1_OFF;
				S2_ON;
				break;
			}
			case (7): 
			{	S0_OFF;		//line selection 1/8 (msb) S2 S1 S0 (lsb)
				S1_ON;
				S2_ON;
				break;
			}
			case (8): 
			{	S0_ON;		//line selection 1/8 (msb) S2 S1 S0 (lsb)
				S1_ON;
				S2_ON;
				break;
			}
		
			default: break;
		}
		SEL_OFF;
		D_ON;
		SEL_ON;
		D_OFF;
	}
}

void eteindreLedBar(int num)
{
		
	
	if ((num>8) || (num<0))
	SEL_ON;	
	else
	{
		
		switch (num)
		{
			case (1): 
			{	S0_OFF;		//line selection 1/8 (msb) S2 S1 S0 (lsb)
				S1_OFF;
				S2_OFF;
				break;
			}
			case (2): 
			{	S0_ON;		//line selection 1/8 (msb) S2 S1 S0 (lsb)
				S1_OFF;
				S2_OFF;
				break;
			}
			case (3): 
			{	S0_OFF;		//line selection 1/8 (msb) S2 S1 S0 (lsb)
				S1_ON;
				S2_OFF;
				break;
			}
			case (4): 
			{	S0_ON;		//line selection 1/8 (msb) S2 S1 S0 (lsb)
				S1_ON;
				S2_OFF;
				break;
			}
			case (5): 
			{	S0_OFF;		//line selection 1/8 (msb) S2 S1 S0 (lsb)
				S1_OFF;
				S2_ON;
				break;
			}
			case (6): 
			{	S0_ON;		//line selection 1/8 (msb) S2 S1 S0 (lsb)
				S1_OFF;
				S2_ON;
				break;
			}
			case (7): 
			{	S0_OFF;		//line selection 1/8 (msb) S2 S1 S0 (lsb)
				S1_ON;
				S2_ON;
				break;
			}
			case (8): 
			{	S0_ON;		//line selection 1/8 (msb) S2 S1 S0 (lsb)
				S1_ON;
				S2_ON;
				break;
			}
		
			default: break;
		}
		SEL_OFF;
		D_OFF;
		SEL_ON;
		
	}
}

void barAllumerNiveau(int niveau)
{
	int i;
	for (i=0; i<niveau;i++)
	{
		allumerLedBar(i+1);
	}
	for (i=8; i>niveau; i--)
	{
		eteindreLedBar(i);
	}

}

void barTousEteindre(void)
{
	int i;
	for (i=0; i<8;i++)
	{
		eteindreLedBar(i+1);
	}
}

void barTousAllumer(void)
{
	int i;
	for (i=0; i<8;i++)
	{
		allumerLedBar(i+1);
	}
}
	

